package com.ch5;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author woldier
 * @version 1.0
 * @description 通过反射获取unsafe对象
 * @date 2023/5/16 10:34
 **/
public class UnsafeAccessible {
    public static Unsafe getUnsafe(){
        Field theUnsafe = null;
        try {
            theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        theUnsafe.setAccessible(true);
        Unsafe o;
        try {
            o = (Unsafe) theUnsafe.get(null);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return o;
    }
}
